<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class b2c_goods_price{

	/**
	 * 更新商品价格
	 * price 销售价
	 * cost 成本价
	 * mktprice 市场价
	 * @param array $pricegroup 包含上面几种价格
	 * @return true false
	 */
	public function update($pricegroup)
	{
		// $example = [
		// 	'bn' => [
		// 		'price'    => 123,
		// 		'cost'     => 456,
		// 		'mktprice' => 789
		// 	],
		// 	...
		// ];

		$objB2cGoods = app::get('b2c')->model('goods');
        $db = kernel::database();
        $db->beginTransaction();
        try {
        	foreach ($pricegroup as $key => $value)
        	{
        		$result = $objB2cGoods->update($value,['bn' => $key]);
        	}
        	$db->commit();
        	return true;
        } catch (\Exception $e) {
            $db->rollBack();
            return false;
        }
	}

	//更新促销商品价格
    public function saveSalesPrice($goods)
    {
	    if(empty($goods['goods_id'])){
            return array(false, "参数错误");
        }
        
        $goodsPmtModel = app::get('b2c')->model('goods_pmt');
        if( $goods['price'] >= $goods['mktprice']){
            $goodsPmtModel->delete(array('goods_id'=>$goods['goods_id']));
            return array(true, "删除");
        }        
        
        $data = array();
        $data['price'] = $goods['price'];
        $data['mktprice'] = $goods['mktprice'];
        $data['last_modify'] = time();
        $goodsPmt = $goodsPmtModel->getRow('*', array('goods_id'=>$goods['goods_id']));
        if($goodsPmt){
            $result = $goodsPmtModel->update($data, array('goods_id'=>$goods['goods_id']));
        }else{
            $data['goods_id'] = $goods['goods_id'];
            $result = $goodsPmtModel->insert($data);
        }
        if($result){
            return array(true, "更新成功");
        }else{
            return array(false, "更新失败");
        }
    }

}